#!/bin/bash
#微服务版本
VERSION="1.0"
#配置文件后缀
ACTIVE=pro
#服务名称
APP="zookeeper-demo"
#服务内存分配
XMS="512m"
XMX="512m"
BASEDIR=$(dirname $(readlink -f $0))
BASEDIR=`cd ${BASEDIR}/..;pwd`
echo ${BASEDIR}
APP_NAME="${APP}-${VERSION}"
JAR_FILE="${BASEDIR}/service/${APP}/${APP_NAME}.jar"
LIB_PATH="${BASEDIR}/service/${APP}/config/,${BASEDIR}/service/${APP}/lib/"
PIDS=`ps -ef |grep $APP_NAME |grep -v grep | awk '{print $2}'`
case $1 in
    start)# 启动
        shift
        if [ "$PIDS" != "" ];then
            echo "${APP_NAME} is running,please stop first!"
            exit 1
        fi
        echo "java -Xms${XMS} -Xmx${XMX} -jar -Dloader.path=${LIB_PATH}  ${JAR_FILE} --spring.profiles.active=${ACTIVE}"
        exec nohup java -Xms${XMS} -Xmx${XMX} -jar -Dloader.path=${LIB_PATH}  ${JAR_FILE} --spring.profiles.active=${ACTIVE} > /dev/null 2>&1 &
        echo "${APP_NAME} is start to running......wait 2s"
        sleep 2
        PIDS=`ps -ef |grep $APP |grep -v grep | awk '{print $2}'`
        if [ "$PIDS" = "" ];then
            echo "${APP_NAME} start fail,look up the log on the path /opt/logs/${APP_NAME}"
            exit 1
        fi
        echo "${APP_NAME}  start success !"
        ;;
    stop)# 停止
        shift
        if [ "$PIDS" = "" ];then
            echo "${APP_NAME} is already stop !"
            exit 1
        fi
        ps -ef | grep $APP | grep -v grep | cut -c 9-15 | xargs kill -s 9
        echo "${APP_NAME} is start to stop......wait 2s"
        sleep 2
        PIDS=`ps -ef |grep $APP |grep -v grep | awk '{print $2}'`
        if [ "$PIDS" != "" ];then
            echo "${APP_NAME} is running,stop fail ,look up the log on the path /opt/logs/${APP_NAME}"
            exit 1
        fi
        echo "${APP_NAME}  is stop success !"
        ;;
    restart) # 重启
        shift
        if [ "$PIDS" != "" ];then
            ps -ef | grep $APP | grep -v grep | cut -c 9-15 | xargs kill -s 9
            sleep 2
        fi
        exec nohup java -Xms${XMS} -Xmx${XMX} -jar -Dloader.path=${LIB_PATH}  ${JAR_FILE} --spring.profiles.active=${ACTIVE} > /dev/null 2>&1 &
        PIDS=`ps -ef |grep $APP_NAME |grep -v grep | awk '{print $2}'`
        if [ "$PIDS" = "" ];then
            echo "${APP_NAME}  restart fail,look up the log on the path /opt/logs/${APP_NAME}"
            exit 1
        fi
        echo "${APP_NAME}  resStart success !"
        ;;
    status)# 查看状态
        shift
        if [ "$PIDS" = "" ];then
            echo "${APP_NAME} is stop !"
        else
            echo "${APP_NAME} is running !"
        fi
        ;;
        *) echo "$0 {start|stop|restart|status}"
        exit 4
        ;;
esac